package com.example.autopia.view.Activities

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.autopia.R
import com.example.autopia.databinding.ActivityNewParkBinding
import com.example.autopia.model.Item.Parking
import com.example.autopia.model.Item.Place
import com.example.autopia.model.Repositories.ParkingRepository
import com.example.autopia.model.Repositories.TestRepository
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions

class NewParkActivity : AppCompatActivity() {
    private lateinit var binding: ActivityNewParkBinding
    private val options = MarkerOptions()
    private val latlngs = ArrayList<LatLng>()
    private val repository = ParkingRepository()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNewParkBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val latitude = 55.791747// Ваша широта
        val longitude = 37.613558 // Ваша долгота

        val latLng = LatLng(latitude, longitude)

        //Менеджер для карты
        val supportMapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        supportMapFragment.getMapAsync { googleMap ->

            val cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 20.5f)
            googleMap.moveCamera(cameraUpdate)
            latlngs.add(LatLng(55.791720, 37.613667))
            latlngs.add(LatLng(55.791720, 37.613640))

            latlngs.add(LatLng(55.791729, 37.613640))
            latlngs.add(LatLng(55.791729, 37.613667))

            latlngs.add(LatLng(55.791738, 37.613640))
            latlngs.add(LatLng(55.791738, 37.613667))

            latlngs.add(LatLng(55.791747, 37.613640))
            latlngs.add(LatLng(55.791747, 37.613667))


            latlngs.add(LatLng(55.791720, 37.613587))
            latlngs.add(LatLng(55.791720, 37.613560))

            latlngs.add(LatLng(55.791729, 37.613560))
            latlngs.add(LatLng(55.791729, 37.613587))

            latlngs.add(LatLng(55.791738, 37.613560))
            latlngs.add(LatLng(55.791738, 37.613587))

            latlngs.add(LatLng(55.791747, 37.613560))
            latlngs.add(LatLng(55.791747, 37.613587))

            var c =0
            for (point in latlngs) {
                c++
                options.position(point!!)
                options.title(c.toString())
                options.snippet("someDesc")
                googleMap.addMarker(options)
            }
        }
        binding.getCoord.setOnClickListener {

            if(binding.etName.text.isEmpty()){
                Toast.makeText(this@NewParkActivity, "Введите название!", Toast.LENGTH_SHORT).show()
            }else if(binding.etAddress.text.isEmpty()){
                Toast.makeText(this@NewParkActivity, "Введите адрес!", Toast.LENGTH_SHORT).show()
            }else if(binding.etDesc.text.isEmpty()){
                Toast.makeText(this@NewParkActivity, "Введите описание парковки!", Toast.LENGTH_SHORT).show()
            }else if(binding.etPrice.text.isEmpty()){
                Toast.makeText(this@NewParkActivity, "Введите цену парковки за час", Toast.LENGTH_SHORT).show()
            }else if(binding.etFreePlaces.text.isEmpty()){
                Toast.makeText(this@NewParkActivity, "Введите количество мест на парковке", Toast.LENGTH_SHORT).show()
            }else{
                binding.showMap.visibility = View.VISIBLE
                binding.getCoord.visibility = View.GONE
                binding.btNext.setOnClickListener {
                    val park = Parking()
                    park.id = "fff"
                    park.placeList=TestRepository.getPlaceListForCreate()
                    park.address=binding.etAddress.text.toString()
                    park.name=binding.etName.text.toString()
                    park.description=binding.etDesc.text.toString()
                    park.allPlaces =binding.etFreePlaces.text.toString().toInt()
                    park.price =binding.etPrice.text.toString().toInt()
                    park.freePlaces=binding.etFreePlaces.text.toString().toInt()
                    repository.addParking(park)
                    Toast.makeText(this@NewParkActivity, "Парковка создана", Toast.LENGTH_SHORT).show()
                    startActivity(Intent(this@NewParkActivity,ParkingListActivity::class.java))
                    finish()
                }
            }
        }
    }
}